package com.qf.swagger1.conf;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfig {

    @Bean
    public Docket userDocket(){

        Docket docket = new Docket(DocumentationType.OAS_30);
        ApiInfo apiInfo=new ApiInfo(
                "用户模块接口文档",
                "用户模块",
                "1.0",
                "http://www.1000phone.com",
                new Contact(
                        "cdm",
                        "http://www.cdm.com",
                        "cdm@1000phone.com"),
                "cdm 2.0",
                "http://www.1000phone.com",
                new ArrayList<>());;
        docket.apiInfo(apiInfo);

        //选择指定模块生成接口文档
        docket = docket.groupName("用户模块")
                .select()
                //指定扫描的包路径
                .apis(RequestHandlerSelectors.basePackage("com.qf.swagger1.controller"))
                //指定具体扫描的接口路径
                .paths(PathSelectors.ant("/user/**"))
                .build();

        //设置一些共性参数
        docket.globalRequestParameters(globalParams());

        return docket;
    }

    //生成共性参数
    private List<RequestParameter> globalParams() {
        List<RequestParameter> requestParameters = new ArrayList<>();

        //共性的token参数
        RequestParameter requestParameter = new RequestParameterBuilder()
                .name("token")
                .description("登录后的认证token信息，先登录才能获取")
                .required(true) //必须的参数
                .in(ParameterType.HEADER)
                .build();

        requestParameters.add(requestParameter);

        return requestParameters;
    }

    @Bean
    public Docket personDocket(){


        Docket docket = new Docket(DocumentationType.OAS_30);
        ApiInfo apiInfo=new ApiInfo(
                "PERSON模块接口文档",
                "PERSON模块",
                "1.0",
                "http://www.1000phone.com",
                new Contact(
                        "cdm",
                        "http://www.cdm.com",
                        "cdm@1000phone.com"),
                "cdm 2.0",
                "http://www.1000phone.com",
                new ArrayList<>());;
        docket.apiInfo(apiInfo);

        //选择指定模块生成接口文档
        docket = docket.groupName("PERSON模块")
                .select()
                //指定扫描的包路径
                .apis(RequestHandlerSelectors.basePackage("com.qf.swagger1.controller"))
                //指定具体扫描的接口路径
                .paths(PathSelectors.ant("/person/**"))
                .build();

        return docket;
    }
}
